package com.hs.srm.admin.modules.notice.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.notice.dao.NoticeMessageDao;
import com.hs.srm.admin.modules.notice.entity.NoticeMessageEntity;
import com.hs.srm.admin.modules.notice.entity.NoticeMessageTouserEntity;
import com.hs.srm.admin.modules.notice.service.NoticeMainInfoService;
import com.hs.srm.admin.modules.notice.service.NoticeMessageService;
import com.hs.srm.admin.modules.notice.service.NoticeMessageTouserService;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.base.modules.sys.entity.SysUserEntity;
import com.hs.srm.base.modules.sys.service.SysUserService;
import com.hs.srm.common.utils.PageUtils;

@Service("noticeMessageService")
public class NoticeMessageServiceImpl extends ServiceImpl<NoticeMessageDao, NoticeMessageEntity> implements
		NoticeMessageService {

	@Autowired
	private NoticeMessageTouserService noticeMessageTouserService;

	@Autowired
	SysUserService sysUserService;

	@Autowired
	private NoticeMainInfoService noticeMainInfoService;

	@Override
	public void publicMessage(int sendKeeper, int sendTeacher, String evaluate) {
		NoticeMessageTouserEntity noticeMessageTouserEntity = new NoticeMessageTouserEntity();
		noticeMessageTouserEntity.setContent(evaluate);
		noticeMessageTouserEntity.setCreateTime(new Date());
		noticeMessageTouserEntity.setPublish(1L);
		noticeMessageTouserEntity.setNoticed(0L);
		if(sendKeeper == 1) {
			noticeMessageTouserEntity.setGroupid(1L);
			this.noticeMessageTouserService.insert(noticeMessageTouserEntity);
			List<SysUserEntity> sysUserEntities = this.sysUserService.selectList(new EntityWrapper<SysUserEntity>()
					.and("del_flag = 0"));
			for(SysUserEntity sysUserEntity : sysUserEntities) {
//				if(null != sysUserEntity.getPost()) {
//					if(sysUserEntity.getPost().contains(Constant.PostType.SHOPOWNER.getKey())) {
//						NoticeMainInfoEntity noticeMainInfoEntity = new NoticeMainInfoEntity();
//						noticeMainInfoEntity.setUserId(sysUserEntity.getUserId());
//						noticeMainInfoEntity.setCreateTime(new Date());
//						noticeMainInfoEntity.setContent(evaluate);
//						noticeMainInfoEntity.setType(SmsConst.SmsTypeEnum.NOTICE.getValue());
//						this.noticeMainInfoService.insert(noticeMainInfoEntity);
//					}
//				}
			}

		}
		if(sendTeacher == 1) {
			noticeMessageTouserEntity.setGroupid(2L);
			this.noticeMessageTouserService.insert(noticeMessageTouserEntity);
		}

	}

	@Override
	public List<NoticeMessageEntity> queryByType(String type) {

		int role = 0;

		if(type.equals("sales")) {
			role = 1;
		}
		if(type.equals("customer")) {
			role = 2;
		}
		if(type.equals("aunt")) {
			role = 3;
		}

		return this.baseMapper.queryByCode(role);
	}

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		Page<NoticeMessageEntity> page = this.selectPage(new QueryParam<NoticeMessageEntity>(params).getPage(),
				new EntityWrapper<NoticeMessageEntity>());

		return new PageUtils(page);
	}

	@Override
	public NoticeMessageEntity selectStateByTemplateCode(String templateCode) {

		return this.baseMapper.selectStateByTemplateCode(templateCode);
	}

	@Override
	public int updateStateByType(String type, Integer id) {
		int status = 0;
		if("msgState".equals(type)) {
			// 查询现有状态
			int nowMsgState = this.baseMapper.selectNowMsgState(id);
			if(nowMsgState == 1) {
				status = this.baseMapper.updateMsgStateByType(id, 0);
			} else {
				status = this.baseMapper.updateMsgStateByType(id, 1);
			}

		} else if("rmdState".equals(type)) {
			int nowRmdState = this.baseMapper.selectNowRmdState(id);
			if(nowRmdState == 1) {
				status = this.baseMapper.updateRmdStateByType(id, 0);
			} else {
				status = this.baseMapper.updateRmdStateByType(id, 1);
			}
		}
		return status;
	}

}
